package xin.clips.web.controller.gy;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.common.enums.gy.WorkOrderStatusEnum;
import xin.clips.common.utils.DateUtils;
import xin.clips.common.utils.ShiroUtils;
import xin.clips.system.domain.gy.GyWorkOrder;
import xin.clips.system.service.gy.IGyWorkOrderService;

import java.util.List;

/**
 * 我的工单信息Controller
 *
 * @author clips
 * @date 2023-03-10
 */
@Controller
@RequestMapping("/gy/myWorkOrder")
public class GyMyWorkOrderController extends BaseController {
    private String prefix = "gy/myWorkOrder";

    @Autowired
    private IGyWorkOrderService gyWorkOrderService;

    @RequiresPermissions("gy:myWorkOrder:view")
    @VisistLog(title = "我的工单信息")
    @GetMapping()
    public String workOrder() {
        return prefix + "/myWorkOrder";
    }

    /**
     * 查询我的工单信息列表
     */
    @RequiresPermissions("gy:myWorkOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyWorkOrder gyWorkOrder, ModelMap mmap) {
        startPage();
        gyWorkOrder.setPrimaryUserId(ShiroUtils.getUserId());
        gyWorkOrder.setOtherUserIds(ShiroUtils.getUserId().toString());
        List<GyWorkOrder> list = gyWorkOrderService.selectMyWorkOrderList(gyWorkOrder);
        for (GyWorkOrder order : list) {
            order.setCurrentUserId(ShiroUtils.getUserId());
        }
        return getDataTable(list);
    }

    /**
     * 申请完成
     */
    @RequiresPermissions("gy:myWorkOrder:edit")
    @GetMapping("/edit/{workId}")
    public String edit(@PathVariable("workId") Long workId, ModelMap mmap) {
        GyWorkOrder workOrder = gyWorkOrderService.selectGyWorkOrderByWorkId(workId);
        mmap.put("workOrder", workOrder);
        return prefix + "/edit";
    }

    /**
     * 更新申请完成工单信息
     */
    @RequiresPermissions("gy:myWorkOrder:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyWorkOrder gyWorkOrder) {
        int ts = DateUtils.differentDaysByMillisecond(gyWorkOrder.getEndTime(), gyWorkOrder.getStartTime());
        if (ts < 0) {
            AjaxResult.error("实际结束时间不能小于实际开始事件");
        }
        return AjaxResult.success(gyWorkOrderService.updateGyWorkOrderFinsh(gyWorkOrder));
    }

    /**
     * 接受任务
     */
    @PostMapping("/reviceWorkOrder")
    @RequiresPermissions("gy:myWorkOrder:reviceWorkOrder")
    @ResponseBody
    public AjaxResult reviceWorkOrder(GyWorkOrder order) {
        GyWorkOrder dbOrder = gyWorkOrderService.selectGyWorkOrderByWorkId(order.getWorkId());
        dbOrder.setWorkStatus(Integer.parseInt(WorkOrderStatusEnum.PROCESSING.getCode())); //设置为处理中
        dbOrder.setProcessUserId(ShiroUtils.getUserId()); //设置处理人id
        return toAjax(gyWorkOrderService.updateGyWorkOrder(dbOrder));
    }

    /**
     * 放弃任务
     */
    @PostMapping("/discard")
    @RequiresPermissions("gy:myWorkOrder:discard")
    @ResponseBody
    public AjaxResult discard(GyWorkOrder order) {
        GyWorkOrder dbOrder = gyWorkOrderService.selectGyWorkOrderByWorkId(order.getWorkId());
        dbOrder.setWorkStatus(Integer.parseInt(WorkOrderStatusEnum.WAIT.getCode())); //设置为待处理
        dbOrder.setProcessUserId(0l); //
        dbOrder.setProcessUserId(ShiroUtils.getUserId()); //设置处理人id
        return toAjax(gyWorkOrderService.updateGyWorkOrder(dbOrder));
    }

    /**
     * 审核详情
     */
    @GetMapping("/detail")
    public String detail(@RequestParam("workId") String workId, ModelMap mmap) {
        GyWorkOrder workOrder = gyWorkOrderService.selectGyWorkOrderByWorkId(Long.valueOf(workId));
        mmap.put("workOrder", workOrder);
        return prefix + "/detail";
    }
}
